package com.hello;

import com.google.common.collect.Lists;
import org.junit.Test;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author : 张国安
 * @datetime : 2018-06-19 13:29:28
 * @desc :
 */
public class ExerciseExample {
    @Test
    public void test() {
        String str = "abcdef";
        char[] chars = str.toCharArray();
        int n = 2;
        char[] rotateLeft = rotate(chars, 0, n - 1);
        System.out.println(new String(rotateLeft));


        LocalDate yesterday = LocalDate.now().minus(1, ChronoUnit.DAYS);
        System.out.println(yesterday);

        String timeFormat = "yyyy-MM-dd HH:mm:ss";
        String endTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern(timeFormat));
        String startTime = LocalDateTime.now().minusDays(1).format(DateTimeFormatter.ofPattern(timeFormat));
        System.out.println(endTime);
        System.out.println(startTime);
    }

    @Test
    public void test1() {
        BigDecimal bigDecimal = new BigDecimal(10);
        BigDecimal bigDecimal1 = new BigDecimal(2);
        System.out.println(bigDecimal.divide(bigDecimal1));

    }

    public static char[] leftRotateString(char[] str, int n) {
        char[] rotateLeft = rotate(str, 0, n - 1);
        char[] rotateRight = rotate(rotateLeft, n, str.length - 1);
        char[] result = rotate(rotateRight, 0, str.length - 1);
        return result;
    }

    /**
     * 将字符串翻转
     *
     * @param str   输入串
     * @param start 字符串中的起始索引(包括)
     * @param end   字符串中的终止索引(包括)
     * @return 翻转后的串
     */
    private static char[] rotate(char[] str, int start, int end) {
        while (start < end) {
            char tmp = str[start];
            str[start] = str[end];
            str[end] = tmp;
            start++;
            end--;
        }
        return str;
    }

    @Test
    public void testList() {
        List<String> list = new ArrayList<>();

        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        List<String> list1 = Lists.newArrayList(list.subList(0, 2));
        list.remove("2");
        for (String str : list1) {
            System.out.println(str);
        }

        List<Integer> list2 = Lists.newArrayList(Arrays.asList(5));
        for (int i = 0; i < 5; i++) {
            list2.stream().map(x -> 5).collect(Collectors.toList());
            //list2.add(5);
        }
        System.out.println(list2);
    }

    @Test
    public void testList2() {
        /*String[] str = new String[]{"a", "b"};
        List list = Arrays.asList(str);
        // 这样调用有问题，内部还是数组结构
        list.add("c");*/

        List<String> a = new ArrayList<String>();
        a.add("1");
        a.add("2");
        for (String temp : a) {
            if ("2".equals(temp)) {
                a.remove(temp);
            }
        }
    }

    @Test
    public void testMap () {
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("userName", "bamboo");

        System.out.println(hashMap.entrySet().iterator().next().getKey());

        //ExecutorService executorService = new ThreadPoolExecutor(5, 10, 5, TimeUnit.SECONDS, );
    }
}
